package helen.core.tools;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import android.util.Base64;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidAlgorithmParameterException;
import java.security.spec.AlgorithmParameterSpec;

public class AES
{

    static String key = "3a0d2b4f69e57c18";

    static byte[] ivBytes = {0x12, 0x34, 0x56, 0x78, (byte) 0x90, (byte) 0xAB,
            (byte) 0xCD, (byte) 0xEF, 0x12, 0x34, 0x56, 0x78, (byte) 0x90,
            (byte) 0xAB, (byte) 0xCD, (byte) 0xEF};

    // public static byte[] encrypt(byte[] ivBytes, byte[] keyBytes, byte[]
    // textBytes)
    // throws java.io.UnsupportedEncodingException,
    // NoSuchAlgorithmException,
    // NoSuchPaddingException,
    // InvalidKeyException,
    // InvalidAlgorithmParameterException,
    // IllegalBlockSizeException,
    // BadPaddingException {
    //
    // AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
    // SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
    // Cipher cipher = null;
    // cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    // cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
    // return cipher.doFinal(textBytes);
    // }
    //
    public static String decrypt(String text)
    {
        try
        {

            byte[] keyBytes = key.getBytes("UTF-8");
            byte[] decode = Base64.decode(text.getBytes("UTF-8"),
                    Base64.DEFAULT);
            AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
            SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
            byte[] decrypt = cipher.doFinal(decode);
            return new String(decrypt, "UTF-8");

        } catch (InvalidKeyException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "";
        } catch (NoSuchAlgorithmException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "";
        } catch (NoSuchPaddingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "";
        } catch (InvalidAlgorithmParameterException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "";
        } catch (IllegalBlockSizeException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "";
        } catch (BadPaddingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "";
        } catch (UnsupportedEncodingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "";
        }
    }
}